package com.cosmic.pizza.web.servlet;

import com.cosmic.pizza.pojo.Food;
import com.cosmic.pizza.pojo.PageBean;
import com.cosmic.pizza.service.FoodService;
import com.cosmic.pizza.service.Impl.FoodServiceImpl;
import com.cosmic.pizza.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/Food/*")
public class FoodServlet extends BaseServlet {
    private FoodService foodService = new FoodServiceImpl();
    //根据种类查询全部
    public void foodShowByType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "6";
        }
        String type = req.getParameter("diff_id");
        PageBean<Food> pageBean =  foodService.findByType(Integer.parseInt(type),Integer.parseInt(currentPage),Integer.parseInt(pageSize));
        writeValue(resp,pageBean);
    }
    //模糊查询
    public void foodShowByFname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "6";
        }
        String fname = req.getParameter("fname");
        PageBean<Food> pageBean = foodService.findByFname(Integer.parseInt(currentPage),Integer.parseInt(pageSize),fname);
        writeValue(resp,pageBean);
    }
    public void foodShowMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String fid = req.getParameter("fid");
        Food food = foodService.findByFid(Integer.parseInt(fid));
        writeValue(resp,food);
    }

}
